import maya.cmds as mc

class LZ_skinCluster():
    def __init__(self):
        pass
       
       
    def listSkinJoints_onMesh( self , object = '' ):
        inputhistory = mc.listHistory(object ,lv =1 )
        for ih in inputhistory:
            if mc.nodeType(ih) == 'skinCluster':
                influenceJnts = mc.skinCluster(ih,q=True,wi=True)
                return influenceJnts
                
        return None
    def findSkinCluster(Self , object = '' ):
        inputhistory = mc.listHistory(object ,lv =1 )
        for ih in inputhistory:
            if mc.nodeType(ih) == 'skinCluster':
                return ih
                
            else:
                return None
                
    def Append_joint_toSkinCluster(self , mesh = '' ,joints = ['','']):
    
        ifskin = self.IfSkined(  inputMesh = mesh )
        
        
        if ifskin == 0:
            mc.skinCluster( joints , mesh ,normalizeWeights = 2 ,toSelectedBones =True )
        else:
            skinNode = self.listSkinCluster(inputMesh = mesh)
            jntInf = self.listAllSkined_joints( skinClus = skinNode )
            
            for jnt in joints:
                if jnt not in  jntInf :
                    mc.skinCluster( skinNode , e=True ,ai = jnt ,lw =True, wt = 0 )
                    
    ##=============================================================================================
    #--------------------- skincluster ------------------------------------------------------------
    
    def IfSkined(self, inputMesh = '' ):
        input = mc.listHistory( inputMesh ,levels=1 )
        inputValue = 0
        for ip in input:
            node = mc.nodeType(ip)
            if node =='skinCluster':
                return 1
        return 0
    
    def listSkinCluster(self, inputMesh = ''):
        input = mc.listHistory( inputMesh  ,levels=1)
        for ip in input:
            node = mc.nodeType(ip)
            if node =='skinCluster':
                return ip
    
    
    def listAllSkined_joints(self,skinClus = ''):
        jnts = mc.skinCluster( skinClus , q=True, inf = True )
        
        return jnts
        
    def connect_Skin_reverseMatrix(self , skin_node = '' , jnt_con = '' ,reverseJnt = '' ):
        #skin_node = self.listSkinCluster(  inputMesh = inputMesh_skined )
        joints_skin = self.listAllSkined_joints(  skinClus = skin_node )

        if jnt_con in joints_skin :
            Connected_attr = mc.listConnections(str(jnt_con)+'.worldMatrix[0]' , p = True ,skipConversionNodes = True )
            for ca in Connected_attr:
                split_Connected_Attr = str(ca).split('.')
                if skin_node == split_Connected_Attr[0]:
                    num_connect = int( str(split_Connected_Attr[1]).split('[')[1].split(']')[0] )

                    if mc.objExists(reverseJnt) == True:
                        mc.connectAttr( str(reverseJnt) + '.worldInverseMatrix[0]' , str( skin_node ) + '.bindPreMatrix['+str(num_connect) + ']' )

                        
                        